package com.mooer.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.res.AssetManager;

/**
 * 文件操作类
 * @author Minglong.Yu
 * @version 1.0 
 * @created Jun 11, 2011 5:08:24 PM
 */
public class FileUtils {
	
	/**
	 * 获取输入流的字符内容
	 * @param is
	 * @return
	 */
	public static String toText(InputStream is){
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		byte[] bytes = new byte[1024];
		int length;
		try {
			while((length=is.read(bytes))!=-1){
				outputStream.write(bytes, 0, length);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				outputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return outputStream.toString();
	}
	
	/**
	 * 从android的assets目录中获取文件内容
	 * @param context
	 * @param fileName
	 * @return
	 */
	public static String getAssertFileContent(Context context, String fileName){
		String text = null;
		AssetManager am = context.getAssets();
		InputStream is=null;
		try {
			is = am.open(fileName);
			text = FileUtils.toText(is);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return text;
	}
}
